package com.yytek.nmp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yytek.common.core.constant.CacheConstants;
import com.yytek.common.core.domain.DeviceCache;
import com.yytek.common.core.utils.bean.BeanCopyUtils;
import com.yytek.common.redis.service.RedisService;
import com.yytek.nmp.domain.IotDeviceInfo;
import com.yytek.nmp.domain.IotDeviceTopo;
import com.yytek.nmp.domain.IotProductInfo;
import com.yytek.nmp.mapper.IotDeviceInfoMapper;
import com.yytek.nmp.mapper.IotDeviceTopoMapper;
import com.yytek.nmp.mapper.IotProductInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class IotDeviceCacheServiceImpl {
    @Autowired
    private RedisService redisService;
    @Resource
    private IotDeviceInfoMapper iotDeviceInfoMapper;
    @Resource
    private IotProductInfoMapper productInfoMapper;
    @Resource
    private IotDeviceTopoMapper iotDeviceTopoMapper;

    /**
     * 初始化缓存信息
     */
    public void driverInitial() {
        //初始化所有使用产品信息
        initUseProduct();
        //初始化加载所有在线设备缓存
        initOnlineDevice();
    }


    /**
     * 初始化加载所有在线设备缓存
     */
    public void initOnlineDevice() {
        //查询所有在线设备
        IotDeviceInfo cxInfo = new IotDeviceInfo();
        cxInfo.setConnectStatus(2);
        List<IotDeviceInfo> onlineList = iotDeviceInfoMapper.selectIotDeviceInfoList(cxInfo);
        if(CollectionUtil.isNotEmpty(onlineList)){
            for (int i = 0; i < onlineList.size(); i++) {
                IotDeviceInfo deviceInfo = onlineList.get(i);
                DeviceCache deviceCache = BeanCopyUtils.copy(deviceInfo,DeviceCache.class);
                //获取产品缓存
                String productKey =  CacheConstants.PRODUCT_USEINFO_KEY+deviceCache.getProductId();
                IotProductInfo productInfo = redisService.getCacheObject(productKey);
                deviceCache.setDataAnalysisDown(productInfo.getDataAnalysisDown());
                deviceCache.setDataAnalysisUp(productInfo.getDataAnalysisUp());
                //是否子设备类型，添加主设备id
                if(deviceCache.getNodeType() == 2){
                    IotDeviceTopo cx = new IotDeviceTopo();
                    cx.setSubId(deviceInfo.getIotId());
                    List<IotDeviceTopo> topoList = iotDeviceTopoMapper.selectIotDeviceTopoList(cx);
                    if(CollectionUtil.isNotEmpty(topoList)){
                        String gateWayId = topoList.get(0).getGatewayId();
                        deviceCache.setGateWayId(gateWayId);
                    }
                }
                //放入设备缓存
                redisService.setCacheObject(CacheConstants.DEVICE_USEINFO_KEY+deviceInfo.getIotId(),
                        deviceCache);
            }
        }
    }

    /**
     * 初始化使用的产品信息
     */
    public void initUseProduct() {
        //查询使用的产品
        IotProductInfo cxInfo = new IotProductInfo();
        List<IotProductInfo> productInfos =  productInfoMapper.selectIotProductInfoList(cxInfo);
        if(CollectionUtil.isNotEmpty(productInfos)){
            for (int i = 0; i < productInfos.size(); i++) {
                IotProductInfo productInfo = productInfos.get(i);
                redisService.setCacheObject(CacheConstants.PRODUCT_USEINFO_KEY+productInfo.getId(),
                        productInfo);
            }
        }
    }
    /**
     * 根据设备id获取全部设备缓存
     * @param id
     * @return
     */
    public DeviceCache getDeviceCacheByIotId(String id) {
        String key = CacheConstants.DEVICE_USEINFO_KEY+id;
        //查询缓存,如果有这个key直接返回
        if(redisService.hasKey(key)){
            //获取设备信息
            DeviceCache deviceCache = getAllDeviceCache(key);
            return deviceCache;
        }else{
            //查询设备缓存信息,查询数据库,放入缓存
            DeviceCache deviceCache = getDevcieCacheByDbAndSetKey(key,id);
            return deviceCache;
        }
    }
    /**
     * 查询设备缓存信息,查询数据库,放入缓存
     * @param key 缓存key
     * @param id 设备id
     * @return
     */
    public DeviceCache getDevcieCacheByDbAndSetKey(String key, String id) {
        IotDeviceInfo deviceInfo = iotDeviceInfoMapper.selectIotDeviceInfoByIotId(id);
        if(deviceInfo != null){
            DeviceCache deviceCache = BeanCopyUtils.copy(deviceInfo,DeviceCache.class);
            //查询出关联产品信息
            IotProductInfo productInfo = productInfoMapper.selectIotProductInfoById(deviceInfo.getProductId());
            deviceCache.setProductKey(productInfo.getProductKey());
            //放入产品缓存
            String productKey =  CacheConstants.PRODUCT_USEINFO_KEY+productInfo.getId();
            redisService.setCacheObject(productKey,productInfo);
            //放入实体返回,不保存到设备缓存
            deviceCache.setDataAnalysisUp(productInfo.getDataAnalysisUp());
            deviceCache.setDataAnalysisDown(productInfo.getDataAnalysisDown());
            //是否子设备类型，添加主设备id
            if(deviceCache.getNodeType() == 2){
                IotDeviceTopo cx = new IotDeviceTopo();
                cx.setSubId(id);
                List<IotDeviceTopo> topoList = iotDeviceTopoMapper.selectIotDeviceTopoList(cx);
                if(CollectionUtil.isNotEmpty(topoList)){
                    String gateWayId = topoList.get(0).getGatewayId();
                    deviceCache.setGateWayId(gateWayId);
                }
            }
            //放入设备缓存
            redisService.setCacheObject(key,deviceCache);
            return deviceCache;
        }else{
            return null;
        }
    }
    /**
     * 获取设备信息的全部信息,包含产品信息
     * @param key
     * @return
     */
    public DeviceCache getAllDeviceCache(String key) {
        DeviceCache deviceCache = redisService.getCacheObject(key);
        //放入产品缓存
        String productKey =  CacheConstants.PRODUCT_USEINFO_KEY+deviceCache.getProductId();
        IotProductInfo productInfo = redisService.getCacheObject(productKey);
        deviceCache.setDataAnalysisDown(productInfo.getDataAnalysisDown());
        deviceCache.setDataAnalysisUp(productInfo.getDataAnalysisUp());
        //是否子设备类型，添加主设备id
        if(deviceCache.getNodeType() == 2){
            IotDeviceTopo cx = new IotDeviceTopo();
            cx.setSubId(deviceCache.getIotId());
            List<IotDeviceTopo> topoList = iotDeviceTopoMapper.selectIotDeviceTopoList(cx);
            if(CollectionUtil.isNotEmpty(topoList)){
                String gateWayId = topoList.get(0).getGatewayId();
                deviceCache.setGateWayId(gateWayId);
            }
        }
        return deviceCache;
    }

    /**
     * 删除设备缓存信息
     * @param iotId
     */
    public void deleteDeviceCacheById(String iotId) {
        String key = CacheConstants.DEVICE_USEINFO_KEY+iotId;
        redisService.deleteObject(key);
    }
}
